package com.yan;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.concurrent.TimeoutException;


/*
    消息生产者
 */
public class Producer {


    public static void main(String[] args) throws Exception, TimeoutException {
        //获取连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri("amqp://root:root@1.116.195.238:5672/%2f");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //声明一个队列    队列名称，是否持久化，是否排他，是否自动删除，消息队列属性信息
        channel.queueDeclare(QueueConstants.workQueueName, true, false, false, null);
        //声明一个交换器   交换器名称，类型，是否持久化，是否自动删除，交换器属性信息
        channel.exchangeDeclare(QueueConstants.exchangeName, BuiltinExchangeType.DIRECT, true, false, null);
        //将消息队列和交换器绑定 并指定路由键
        channel.queueBind(QueueConstants.workQueueName, QueueConstants.exchangeName, QueueConstants.routingKeyName);

        for (int i = 0; i < 15; i++) {
            //向队列发送消息 交换器名字，路由键，消息内容属性，消息的字节数组
            channel.basicPublish(QueueConstants.exchangeName, QueueConstants.routingKeyName, null, ("工作队列：" + i).getBytes());
        }

        channel.close();
        connection.close();

    }

}
